package zhihui.mobile.presenter.shop;

import android.content.Context;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.bean.shop.address.ShoppingAddress;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.shop.ShopAddressManager;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.shop.address.IShopAddressManagerContract;

/**
 * Created by 张公铭 on 2017/8/15 0015.
 */

public class ShopAddressManagerPresenter implements IShopAddressManagerContract.IShopAddressManagerPresenter
{
    private Context mContext;
    private IShopAddressManagerContract.IShopAddressManagerView addressManagerView;

    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        addressManagerView = (IShopAddressManagerContract.IShopAddressManagerView) view;
    }

    @Override
    public void onResume()
    {

    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void initData()
    {
        addressManagerView.refreshList(ShopAddressManager.getInstance().getmList());
    }

    @Override
    public void delete(final ShoppingAddress address)
    {
        addressManagerView.showLoading();
        ShopAddressManager.getInstance().delete(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                addressManagerView.hindLoading();
                addressManagerView.showMessage("删除成功！");
                ShopAddressManager.getInstance().getmList().remove(address);
                addressManagerView.refreshList(ShopAddressManager.getInstance().getmList());
            }

            @Override
            public void failed(String message)
            {
                addressManagerView.hindLoading();
                addressManagerView.showMessage(message);
            }
        }, address.getId());
    }

    @Override
    public void setDefault(final ShoppingAddress address, boolean def)
    {
        if (!def)
        {
            return;
        }
        addressManagerView.showLoading();
        ShopAddressManager.getInstance().setDefault(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                addressManagerView.hindLoading();
                for (int i = 0; i < ShopAddressManager.getInstance().getmList().size(); i++)
                {
                    ShopAddressManager.getInstance().getmList().get(i).setIsdefault(0);
                    if (address.getId() == ShopAddressManager.getInstance().getmList().get(i).getId())
                    {
                        ShopAddressManager.getInstance().getmList().get(i).setIsdefault(1);
                    }
                }
                addressManagerView.refreshList(ShopAddressManager.getInstance().getmList());
            }

            @Override
            public void failed(String message)
            {
                addressManagerView.hindLoading();
                addressManagerView.showMessage(message);
            }
        }, address.getId());
    }
}
